package com.xg.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xg.common.constant.CommonStatusEnum;
import com.xg.common.request.SeriesReq;
import com.xg.common.result.ApiResult;
import com.xg.common.util.Func;
import com.xg.common.util.PageUtil;
import com.xg.common.util.PageUtilHelper;
import com.xg.entity.UsedCar;
import com.xg.entity.UsedCarBrand;
import com.xg.entity.UsedCarSeries;
import com.xg.mapper.UsedCarBrandMapper;
import com.xg.mapper.UsedCarSeriesMapper;
import com.xg.service.IUsedCarSeriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jdh
 * @since 2023-03-30
 */
@Service
public class UsedCarSeriesServiceImpl extends ServiceImpl<UsedCarSeriesMapper, UsedCarSeries> implements IUsedCarSeriesService {
    @Autowired
    private UsedCarSeriesMapper usedCarSeriesMapper;

    @Autowired
    private UsedCarBrandMapper usedCarBrandMapper;

    @Override
    public List<UsedCarSeries> queryUsedCarSeriesList() {
        return usedCarSeriesMapper.queryUsedCarSeriesList();
    }

    @Override
    public ApiResult<PageUtil<UsedCarSeries>> querySeriesList(SeriesReq seriesReq) {
        List<UsedCarSeries> list = usedCarSeriesMapper.querySeriesList(seriesReq);
        PageUtil<UsedCarSeries> pageUtil = PageUtilHelper.pageHelper(list, seriesReq.getCpage(), seriesReq.getPageSize());
        return ApiResult.success(pageUtil);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiResult deleteSerices(List<Integer> ids) {
        int i = usedCarSeriesMapper.deleteBatchIds(ids);
        if (i > 0) {
            return ApiResult.success();
        }
        return ApiResult.error(CommonStatusEnum.SERIES_DELETE_ERROR.getCode(), CommonStatusEnum.SERIES_DELETE_ERROR.getMsg());
    }

    @Override
    public ApiResult<UsedCarSeries> selectSeriesInfo(Integer seriesId) {
        UsedCarSeries usedCarSeries = usedCarSeriesMapper.selectById(seriesId);
        if (!Func.isEmpty(usedCarSeries)){
            return ApiResult.success(usedCarSeries);
        }else {
            return ApiResult.error(CommonStatusEnum.SERIES_INFO_ERROR.getCode(), CommonStatusEnum.SERIES_INFO_ERROR.getMsg());
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiResult saveSeries(UsedCarSeries usedCarSeries) {
        int insert = 0;
        int update = 0;
        if (Func.isEmpty(usedCarSeries.getSeriesId())) {
//            usedCarSeries.setCreateTime(new Date());
            insert = usedCarSeriesMapper.insert(usedCarSeries);
        } else {
//            usedCarSeries.setUpdateTime(new Date());
            update = usedCarSeriesMapper.updateById(usedCarSeries);
        }
        if (insert>0||update>0) {
            return ApiResult.success();
        } else {
            return ApiResult.error(CommonStatusEnum.SERIES_SAVE_ERROR.getCode(), CommonStatusEnum.SERIES_SAVE_ERROR.getMsg());
        }
    }

    @Override
    public ApiResult<List<UsedCarSeries>> findListByBrandId(Integer brandId) {
        List<UsedCarSeries> list = usedCarSeriesMapper.selectList(Wrappers.<UsedCarSeries>lambdaQuery().eq(UsedCarSeries::getBrandId, brandId));
        return ApiResult.success(list);
    }

    @Override
    public ApiResult<UsedCarBrand> findBrandBySeriesId(Integer seriesId) {
        UsedCarSeries usedCarSeries = usedCarSeriesMapper.selectById(seriesId);
        UsedCarBrand usedCarBrand = usedCarBrandMapper.selectById(usedCarSeries.getBrandId());
        return ApiResult.success(usedCarBrand);
    }

    @Override
    public List<UsedCar> queryUsedCarSeriesById(Integer seriesId) {
        return usedCarSeriesMapper.queryUsedCarSeriesById(seriesId);
    }

    @Override
    public List<UsedCarSeries> queryUsedCarSeriesListTwo() {
        return usedCarSeriesMapper.queryUsedCarSeriesListTwo();
    }


}
